using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace WorkflowTest
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("🚀 开始WorkflowCore工作流审批功能测试");
            Console.WriteLine("=" * 50);

            var client = new HttpClient();
            var baseUrl = "http://localhost:8080";

            try
            {
                // 测试登录
                Console.WriteLine("🔐 测试登录...");
                var loginData = new
                {
                    username = "admin",
                    password = "1q2w3E*"
                };

                var loginJson = JsonSerializer.Serialize(loginData);
                var loginContent = new StringContent(loginJson, Encoding.UTF8, "application/json");
                
                var loginResponse = await client.PostAsync($"{baseUrl}/api/account/login", loginContent);
                var loginResult = await loginResponse.Content.ReadAsStringAsync();
                
                Console.WriteLine($"登录响应: {loginResult}");
                
                if (loginResponse.IsSuccessStatusCode)
                {
                    var loginResponseObj = JsonSerializer.Deserialize<LoginResponse>(loginResult);
                    if (loginResponseObj?.Success == true)
                    {
                        Console.WriteLine("✅ 登录成功");
                        
                        // 设置Authorization header
                        client.DefaultRequestHeaders.Add("Authorization", $"Bearer {loginResponseObj.AccessToken}");
                        
                        // 测试获取工作流实例列表
                        Console.WriteLine("\n📋 测试获取工作流实例列表...");
                        var instancesResponse = await client.GetAsync($"{baseUrl}/api/app/workflow/instances");
                        var instancesResult = await instancesResponse.Content.ReadAsStringAsync();
                        Console.WriteLine($"工作流实例列表响应: {instancesResult}");
                        
                        // 测试获取待审批列表
                        Console.WriteLine("\n⏳ 测试获取待审批列表...");
                        var pendingResponse = await client.GetAsync($"{baseUrl}/api/app/workflow/pending-approvals");
                        var pendingResult = await pendingResponse.Content.ReadAsStringAsync();
                        Console.WriteLine($"待审批列表响应: {pendingResult}");
                        
                        // 测试创建入库单
                        Console.WriteLine("\n📦 测试创建入库单...");
                        var inboundData = new
                        {
                            code = $"IN{DateTime.Now:yyyyMMddHHmmss}",
                            name = "测试入库单",
                            price = 1000.00,
                            state = 2,
                            remark = "WorkflowCore测试入库单"
                        };
                        
                        var inboundJson = JsonSerializer.Serialize(inboundData);
                        var inboundContent = new StringContent(inboundJson, Encoding.UTF8, "application/json");
                        var inboundResponse = await client.PostAsync($"{baseUrl}/api/app/inboundmanagement/create", inboundContent);
                        var inboundResult = await inboundResponse.Content.ReadAsStringAsync();
                        Console.WriteLine($"入库单创建响应: {inboundResult}");
                    }
                    else
                    {
                        Console.WriteLine("❌ 登录失败");
                    }
                }
                else
                {
                    Console.WriteLine($"❌ 登录请求失败: {loginResponse.StatusCode}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"❌ 测试异常: {ex.Message}");
            }
            
            Console.WriteLine("\n" + "=" * 50);
            Console.WriteLine("✅ WorkflowCore工作流审批功能测试完成");
        }
    }
    
    public class LoginResponse
    {
        public bool Success { get; set; }
        public string AccessToken { get; set; }
        public string Message { get; set; }
    }
} 